﻿
var serializer = new JSONSerializer();

$(document).ready(function () {

    //取消用户筛选条件
    $(".btnDelLender").bind("click", function () {
        $(".txtUserName").val("");
        $(".txtUserId").val("");
    });

    //搜索用户
    $(".btnSearchBorrow").bind("click", function () {
        var $userName = $(".txtName").val();
        var $phone = $(".txtPhone").val();
        $.getJSON("../Handler/FinancialUser.ashx",
            { UserName: $userName, Phone: $phone, type: 10, _: Math.random() },
            function (result) {
                $(".userInfo").empty();
                if (result.Status) {
                    $(result.Data).each(function (index, item) {
                        $("#UserTemplete").tmpl(item).appendTo($(".userInfo"));
                    });
                    $(".userInfo tr").css("cursor", "pointer").click(function () {
                        $(".txtUserName").val($(this).find(".name").text());
                        $(".txtUserId").val($(this).find(".userId").text());
                    });
                } else {
                    alert(result.Message);
                }
            });
    });
    $(".txtUserName").bind("click", function () {
        $(".btnShow").click();
    });

    //设置分页信息
    var page = new Page();
    page.Url = "../Handler/FinancialOrder.ashx";
    page.Dom = $("#searchArea");
    page.type = 0;
    page.CallBack = function (data) {
        $(".borrowList").empty();
        $.each(data, function (index, item) {
            $("#ContractTemplete").tmpl(item).appendTo($(".borrowList"));
        });
    };
    page.GetPageData();

    //弹出已匹配既有债权详情
    $(".borrowList").on("click", ".view", function () {

        var finaOrderId = $(this).attr('data-id');
        //绑定详情
        $.ajax({
            type: "post",
            url: '../Handler/FinancialOrder.ashx',
            dataType: "json",
            data: { Id: finaOrderId, type: 4, _: Math.random() },
            async: false,
            cache: false,
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    if (result.Data.Order) {
                        $(':hidden[name=FinaOrderId]').val(result.Data.Order.OrderId);
                        $(".divAmount").text(result.Data.Order.Amount * 10000);
                        $(".divIncome").text(result.Data.Order.Income);//收益
                        $(".divTerm").text(result.Data.Order.Term);
                        $(".divFee").text(result.Data.Order.Fee);
                        $(".divContractStartDate").text(result.Data.Order.ContractStartDate);//初始出借日期
                        $(".divMemo").text(result.Data.Order.Memo);
                        $(".divPayBank").text(result.Data.Order.PayBankName);
                        $(".divDepositBank").text(result.Data.Order.DepositBank);
                        $(".divRecAccName").text(result.Data.Order.RecAccName);
                        $(".divRecAccNum").text(result.Data.Order.RecAccNum);
                        $(".divAgreeNum").text(result.Data.Order.AgreeNum);
                        $(".divFinaNum").text(result.Data.Order.OrderId);
                        $(".divProductType").text(result.Data.Order.Product);
                    }
                    if (result.Data.User) {
                        $(".divUserId").text(result.Data.User.Name);
                    }
                    //预加载债权匹配信息列表
                    $.ajax({
                        type: "post",
                        url: "../Handler/CreditMatchHandler.ashx",
                        dataType: "json",
                        data: { FinaOrderId: finaOrderId, type: 0, _: Math.random() },
                        async: true,
                        cache: false,
                        complete: function () {
                        },
                        success: function (result) {
                            if (result.Status) {
                                $(".tblBorrowRecord .rowBorrowRecord").remove();
                                var $Total = 0;
                                $.each(result.Data, function (index, item) {
                                    $Total += parseFloat(item.LoanMatchAmount);
                                    $("#MatchCreditTemplete").tmpl(item).insertBefore($("#rowMatchAmountTotal"));
                                });
                                $("#totalMatchAmount").text($Total.toFixed(2));
                            }
                        }
                    });
                    //单独绑定理财规模*10000
                    $("div[data-name='Amount']").text(result.Data.Amount * 10000);
                    $("#showResult").show();
                }
            }
        });
    });


});